//
//  SHMenuCollectionController.m
//  吃货·上海
//
//  Created by lanou on 15/11/24.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "SHMenuCollectionController.h"
#import "SHMenuCollectionViewCell.h"
#import "PrefixHeader.pch"
#import "SHMenuController.h"
#import <BmobSDK/BmobFile.h>
#import "UIImageView+WebCache.h"
#import "DetailController.h"
#import "MBProgressHUD.h"


@interface SHMenuCollectionController () <UICollectionViewDataSource ,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@end

@implementation SHMenuCollectionController

static NSString * cellIdentifier = @"Cell";

- (NSMutableArray *)modelArray
{
    if (_modelArray == nil) {
        self.modelArray = [NSMutableArray array];
    }
    
    return _modelArray;
}

- (void)loadView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((KScreenWidth - 40.1) / 3, (KScreenWidth - 40.1) / 3);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    
    UICollectionView *collection = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView = collection;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[SHMenuCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
    self.collectionView.backgroundColor  =[UIColor whiteColor];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent =NO;
    
    [self getDataFromMyDB];
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailController *detailVc  =[[DetailController alloc]init];
    detailVc.model  = self.modelArray[indexPath.item];
    [self.navigationController pushViewController:detailVc animated:NO];
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _modelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SHMenuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    
    cell.model = self.modelArray[indexPath.item];
    
    cell.backgroundColor = KMyColor;
    
    cell.backgroundColor  = [UIColor greenColor];
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 10, 10, 10);
}

#pragma mark - 数据请求
- (void)getDataFromMyDB
{
    BmobQuery *bqeury = [BmobQuery queryWithClassName:@"Menu"];
    [bqeury findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        
        for (BmobObject *obj in array) {
            SHMenuModel *model =[[SHMenuModel alloc]init];
            
            model.title = [obj objectForKey:@"Dishname"];
            BmobFile *file = [obj objectForKey:@"icon"];
            model.imageUrl = file.url;
            model.info = [obj objectForKey:@"ifno"];
            model.myClass = [obj objectForKey:@"class"];
            model.mainmatcrials = [obj objectForKey:@"mainmatcrials"];
            
            [self.modelArray addObject:model];
            
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.collectionView reloadData];
        });
    }];
}


@end
